import libreface
import os
import shutil

def process_video_files(parent_dir,output_dir):

    # 确保输出目录存在，不存在则创建
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
        print(f"Created output directory: {output_dir}")

    # 获取所有视频文件
    video_files = []
    for root, dirs, files in os.walk(parent_dir):
        for file in files:
            if file.endswith(('.mp4', '.avi', '.mov')):
                video_files.append(os.path.join(root, file))

    # 处理视频文件
    total_files = len(video_files)
    for idx, video_path in enumerate(video_files, start=1):
        output_save_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_path))[0] + '_features.csv')

        # 输出处理进度信
        print(f"Processing file {idx}/{total_files}: {video_path}")

        # 获取面部特征并保存为 CSV 文件
        libreface.get_facial_attributes(video_path, output_save_path=output_save_path, temp_dir="../temp")

        # 完成输出
        print(f"Finished processing: {output_save_path}\n")

        temp_dir = "../temp"
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)


if __name__ == '__main__':
    parent_dir = '../error/video-labeled'  # 设定父目录路径
    process_video_files(parent_dir, output_dir='../error/out_feature')
